package lyo.levelup.thread;
public class CipherCesar {
    private String str = "";
    private String encrString = "";
    private int count=0;
    private String word;
    private int key;
    private char temp;
    public CipherCesar(String str, int key)
    {
        this.word = str;
        this.key = key;
    }
    public String enCrString()
    {
        for(char c='A'; c<='Z'; c++)
        {
            str+=c;
        }      
        try {
    char []alphabet = str.toCharArray();
    char []alphabetNew = new char [alphabet.length];
              for(int i = 0; i < alphabet.length+1; i++)
              {
                  if(i <= key-1)
                  {
                     alphabetNew[alphabetNew.length-1-i] = alphabet[i];
                  }
                  else
                  {
                    System.arraycopy(alphabet, i, alphabetNew, i-key,1);
                  }
              }
            for(int i = alphabetNew.length-key; i < alphabetNew.length; i++)
              {
                  for(int j = i+1; j < alphabetNew.length; j++) 
                  {
                    temp=alphabetNew[i];                  
                    alphabetNew[i]=alphabetNew[j];
                    alphabetNew[j]=temp;
                  }
              }       
             while(count < word.length())
             {                 
             for(int i = 0; i < alphabet.length; i++)
             {                 
                  for(int j = 0; j < alphabetNew.length; j++)
                  {              
                        if(word.charAt(count) == alphabet[i] && i == j)
                        {
                            encrString += alphabetNew[j]; 
                        }                          
                  }
             }
                 count++;
             }             
        }        
        catch(Exception e)
        {
            System.out.println("System error, please call to system administrators");
        }
        return encrString;
     }
    public void output()
    {
      System.out.println("You encrypt word is "+this.enCrString());
    }
}
